﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CameraCtrl : MonoBehaviour
{
        public GameObject mainCamera;

        [Header("旋转速度")]
        public float rotateSpeed = 50;

        [Header("鼠标中键图标")]
        public Texture2D cursorTex;
        
        
        private bool isEnableRotate = false;

        void Start()
        {
                if (mainCamera != null)
                {
                        mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
                }
        }

        public void isDown()
        {
                isEnableRotate = true;
        }

        public void Touching()
        {
                AutoRotate.closeAutoFlag = true;
        }

        public void isUp()
        {
                isEnableRotate = false;
        }

        float x,y;
        void Update()
        {
                if (isEnableRotate)
                {
                        x = Input.GetAxis("Mouse X");
                        y = Input.GetAxis("Mouse Y");
                        CameraRotate(x, y);
                }
        }

        /// <summary>
        /// 左键控制旋转
        /// </summary>
        /// <param name="_mouseX"></param>
        /// <param name="_mouseY"></param>
        public void CameraRotate(float _mouseX, float _mouseY)
        {
                //注意!!! 此处是 GetMouseButton() 表示一直长按鼠标左键；不是 GetMouseButtonDown()
                if (Input.GetMouseButton(0))
                {
                        Vector3 centerPoint = mainCamera.transform.parent.position;
                        //控制相机绕中心点(centerPoint)水平旋转
                        mainCamera.transform.RotateAround(centerPoint, Vector3.up, -_mouseX * rotateSpeed);
                        //控制相机绕中心点垂直旋转(！注意此处的旋转轴时相机自身的x轴正方向！)
                        mainCamera.transform.RotateAround(centerPoint, mainCamera.transform.right, _mouseY * rotateSpeed);
                }
        }

}
